package com.gsit.expenditure.main;

import java.util.List;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Screen;
import javafx.stage.Stage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gsit.expenditure.domain.Invoice;
import com.gsit.expenditure.services.CustomerService;
import com.gsit.expenditure.services.InvoiceService;
import com.gsit.expenditure.utils.AppContextLoader;
import com.gsit.expenditure.utils.InvoiceStatus;
import com.gsit.expenditure.utils.Page;
import com.gsit.expenditure.utils.ViewHandler;
import com.gsit.expenditure.view.ApplicationPreloader;

public class ExpApp extends Application {

	private Stage stage;
	private static ApplicationPreloader applicationPreloader = null;
	private static final Logger logger = LoggerFactory.getLogger(ExpApp.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		logger.info(System.getProperty("user.home"));
		String usrHome= System.getProperty("user.home");
		System.setProperty("app.root", usrHome+"/ExpenditureApp/logs");
		applicationPreloader = new ApplicationPreloader();
		
		Application.launch(ExpApp.class, args);
		
		//new ExpApp().oldVoid();

	}

	private void oldVoid() {
		/*OrderService orderServices = AppContextLoader
				.getBean(OrderService.class);
		orderServices.addDummyOrder("v1");
		orderServices.addDummyOrder("v2");

		List<Order> orders = orderServices.findAllOrder();
		for (Order order : orders) {
			System.out.println("The order name is " + order.getCustomer());
		}*/
		//CUSTOMER
		 
		/*//VENDOR
		VendorServices vendorServices=AppContextLoader.getBean(VendorServices.class);
		Vendor vendor=new Vendor();
		vendorServices.saveOrUpdate(vendor);
		
		
		//VEHICLE
		VehicleServices vehicleServices=AppContextLoader.getBean(VehicleServices.class);
		Vehicle vehicle=new Vehicle();
		
		vehicleServices.saveOrupdate(vehicle);
		*/
		
		InvoiceService invoiceService = AppContextLoader.getBean(InvoiceService.class);
		//List<Invoice> invoices = invoiceService.findAll();
		Invoice invoice = new Invoice();
		invoice.setInvoiceNumber("2");
		invoice.setStatus(InvoiceStatus.UNPAID);
		List<Invoice> invoices = invoiceService.findInvoices(invoice, "", "", "dfgd", "mh2323");
		
		for (Invoice invoice1 : invoices) {
			System.out.println("Invoices are :: "+invoices.size() +"   "+invoice1 );
			System.out.println("Bills are :: "+invoice1.getBills().iterator().next() );
		}
		
	}
	private void loadContextLoader(){
		AppContextLoader.getBean(CustomerService.class); 
	}

	@Override
	public void start(Stage mainStage) throws Exception {
		
		//applicationPreloader.start(new Stage());
		logger.info("ExpApp is loading");
		stage = mainStage;
		stage.setX(0);
		stage.setY(0);
		ViewHandler.setMainStage(mainStage);
		
		
		//Screen screen = new Screen();
		Screen screen = Screen.getPrimary();
		Rectangle2D screenBounds =screen.getVisualBounds();
		stage.setWidth(screenBounds.getWidth()  );
		stage.setHeight(screenBounds.getHeight() );
			
	
		Parent root = (Parent) ViewHandler.loadPage(Page.MAIN_PAGE);
		
		Scene scene = new Scene(root,screenBounds.getWidth(),screenBounds.getHeight());
		
		//scene.getStylesheets().add(ExpApp.class.getResource("/demoshowhide/demo.css").toExternalForm());
		scene.getStylesheets().add(ExpApp.class.getResource("/css/main/style.css").toExternalForm());
		stage.setScene(scene);
		stage.getIcons().add(new Image("/image/logo.png",50,50,true,true)); 
		stage.getScene().setRoot(root);
		stage.setTitle("Exp Application V 1.0");
		stage.sizeToScene();
		stage.show();
		
		loadContextLoader();
		
		//applicationPreloader.stop();
		
	}

}
